package com.mallcai.bigdata.ladon.controller.report;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.controller.report.form.GroupForm;
import com.mallcai.bigdata.ladon.dpl.entity.report.Group;
import com.mallcai.bigdata.ladon.service.report.IGroupService;
import com.mallcai.bigdata.ladon.vo.Pair;
import com.mallcai.bigdata.ladon.vo.system.TreeNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 报表组管理
 * Created by oneape<oneape15@163.com>
 * Created 2019-07-09 10:45.
 * Modify:
 */
@RequestMapping("/report/group")
@RestController
public class GroupController {

    @Autowired
    private IGroupService iGroupService;

    @RequestMapping("/add")
    public String addModel(@RequestBody @Validated(value = GroupForm.AddCheck.class) GroupForm form) {
        Group group = form.toEntity();

        int status = iGroupService.addGroup(group);
        if (status <= 0) {
            throw new BusinessException("添加报表组失败");
        }
        return "添加报表组成功";
    }

    @RequestMapping("/edit")
    public String editModel(@RequestBody @Validated(value = GroupForm.EditCheck.class) GroupForm form) {
        Group group = form.toEntity();

        int status = iGroupService.editGroup(group);
        if (status <= 0) {
            throw new BusinessException("修改报表组失败");
        }

        return "修改报表组成功";
    }

    @RequestMapping("/del")
    public String delModel(@RequestBody @Validated(value = GroupForm.IdCheck.class) GroupForm form) {
        Group group = form.toEntity();
        int status = iGroupService.delGroup(group);
        if (status <= 0) {
            throw new BusinessException("删除报表组失败");
        }

        return "删除报表组成功";
    }

    @RequestMapping("/getById")
    public Group getModelById(@RequestBody @Validated(value = GroupForm.IdCheck.class) GroupForm form) {
        Group group = iGroupService.getById(form.getGroupId());
        if (group == null) {
            throw new BusinessException("查询报表组失败");
        }

        return group;
    }

    @RequestMapping("/getByPage")
    public List<Group> getByPage(@RequestBody @Validated GroupForm form) {
        Map<String, Object> params = form.getParams();
        if (params == null) {
            params = new HashMap<>();
        }

        List<Group> groups = iGroupService.getByParams(params);
        Pair<Integer, Map<Integer, List<Group>>> pair = iGroupService.splitGroup(groups);

        // 从最深的分支开始向根节点递归
        Map<Long, List<Group>> preNode = new LinkedHashMap<>();
        for (int i = pair.getLeft(); i > 0; i--) {
            Map<Long, List<Group>> curNode = new LinkedHashMap<>();

            List<Group> tmp = pair.getRight().get(i);
            if (tmp == null) {
                continue;
            }

            for (Group g : tmp) {
                if (preNode.containsKey(g.getId())) {
                    g.setChildren(preNode.get(g.getId()));
                }
                if (!curNode.containsKey(g.getParentId())) {
                    curNode.put(g.getParentId(), new ArrayList<>());
                }
                curNode.get(g.getParentId()).add(g);
            }

            preNode = curNode;
        }

        // 最终的根节点Id为0, 所以这里只取0
        List<Group> allGroup = new ArrayList<>();
        preNode.values().forEach(l -> allGroup.addAll(l));
        return allGroup;
    }

    /**
     * 获取报表组树形结构
     *
     * @return ApiResult
     */
    @RequestMapping("/treeNodes")
    public List<TreeNodeVo> getTreeNodes() {
        return iGroupService.getTreeNodes();
    }
}
